package com.yidu.logisticssystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.logisticssystem.entity.LogisticsCar;
import com.yidu.logisticssystem.entity.LogisticsMap;
import com.yidu.logisticssystem.entity.LogisticsStaff;
import com.yidu.logisticssystem.entity.vo.CarVo;
import com.yidu.logisticssystem.service.LogisticsCarService;
import com.yidu.logisticssystem.service.LogisticsMapService;
import com.yidu.logisticssystem.utils.BaseMessage;
import com.yidu.logisticssystem.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车辆信息表(LogisticsCar)表控制层
 *
 * @author 陈子睿
 * @since 2021-03-12 14:34:50
 */
@Api(tags = "车辆信息表")
@RestController
@RequestMapping("logisticsCar")
public class LogisticsCarController {
    /**
     * 树结构
     */
    @Resource
    private LogisticsMapService logisticsMapService;
    /**
     * 服务对象
     */
    @Resource
    private LogisticsCarService logisticsCarService;

  

    /**
     * 查询所有车辆信息,地区字符串截取
     * @param carVo
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询所有车辆信息")
    @GetMapping("selectAll")
    public Map<String, Object> findAll(CarVo carVo, int page, int limit) {
        //定义一个str
        String str = "";
        //new一个Map
        Map<String, Object> map = new HashMap<>();
        //车辆vo,查询全部返回list
        List<CarVo> list = logisticsCarService.queryAll(carVo);
        //将参数传入到分页助手
        PageHelper.startPage(page, limit);
        //分页查询到集合之后，将集合数据封装到分页助手的PageInfo对象中
        PageInfo pageInfo = new PageInfo(list);
        //循环list，判断车辆地址长度
        for (CarVo car : list) {
            //当地址长度为2
            if (car.getMapId().length() == 2) {
                //则根据地址查询，返回一个Map对象
                LogisticsMap logisticsMap = logisticsMapService.queryById(car.getMapId());
                //将查询出来的地址名赋给str
                str = logisticsMap.getMapName();
                //当地址长度等于3
            } else if (car.getMapId().length() == 3) {
                //selectfufind 根据车辆地址id查询上级
                List<LogisticsMap> listUp = logisticsMapService.selectfufind(car.getMapId());
                //根据车辆地址查询，该行数据，返回一个Map对象
                LogisticsMap logisticsMapTwo = logisticsMapService.queryById(car.getMapId());
                //循环listUp返回的数据
                for (LogisticsMap logisticsMap : listUp) {
                    //将查询到的名字给str
                    str = logisticsMap.getMapName();
                    //在str后累加logisticsMapTwo里的地区名
                    str += logisticsMapTwo.getMapName();
                }
                //如果长度为大于3
            } else if (car.getMapId().length() > 3) {
                //根据车辆地址查找上级返回listTwo集合
                List<LogisticsMap> listTwo = logisticsMapService.selectfufind(car.getMapId());
                //根据地址id查询，返回一个对象logisticsMapTwo
                LogisticsMap logisticsMapTwo = logisticsMapService.queryById(car.getMapId());
                //循环listTwo，泛型LogisticsMap
                for (LogisticsMap LogisticsMap : listTwo) {
                    //根据地区Id，selectfufind方法查询上级，返回集合LogisticsMapTwo
                    List<LogisticsMap> LogisticsMapTwo = logisticsMapService.selectfufind(LogisticsMap.getMapId());
                    //循环集合LogisticsMapTwo
                    for (LogisticsMap LogisticsMapThree : LogisticsMapTwo) {
                        //将查询出来的名字赋值给str
                        str = LogisticsMapThree.getMapName();
                        //将外层循环的地区名，累加赋值给str
                        str += LogisticsMap.getMapName();
                        //将最原本的地区名称累加赋值给str
                        str += logisticsMapTwo.getMapName();
                    }
                }
            }
            //car对象的地区名称设置为str的值
            car.setMapName(str);
        }

        //查询行数
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", pageInfo.getList()); // 这里可以直接写list
        map.put("count", pageInfo.getTotal()); // 总行数自动生成
        return map;
    }

    /**
     * 添加或者修改
     *
     * @param carId
     * @param logisticsCar
     * @return
     */
    @ApiOperation(value = "添加或修改")
    @PostMapping("addOrUpdate")
    public BaseMessage addOrUpdate(Integer carId, LogisticsCar logisticsCar) {
        //判断carId是否为空
        if (carId == null) {
            logisticsCarService.insertSelective(logisticsCar);
        } else {
            logisticsCarService.update(logisticsCar);
        }
        return BaseMessage.succes();
    }

    /**
     * 修改赋值
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "修改赋值，根据Id查询")
    @PostMapping("update")
    public BaseMessage update(Integer id) {
        LogisticsCar car = logisticsCarService.queryById(id);
        return BaseMessage.succes(car);
    }


    /**
     * 删除
     *
     * @param carId
     * @return
     */
    @ApiOperation(value = "根据Id删除车辆信息")
    @PostMapping("delete")
    public BaseMessage delete(Integer carId) {
        return logisticsCarService.deleteById(carId) > 0 ?
                BaseMessage.succes() : BaseMessage.fail();
    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据Id批量删除，车辆信息")
    @PostMapping("deleteAll")
    public BaseMessage deleteAll(String ids) {
        return logisticsCarService.deleteAll(ids) > 0 ?
                BaseMessage.succes() : BaseMessage.fail();
    }


    /**
     * 通过车辆当前所在地查询单条数据
     *
     * @return 单条数据
     */
    @ApiOperation(value = "根据mapId，查询当前地址空闲车辆")
    @GetMapping("queryMapId")
    public Map<String, Object> queryMapId(HttpSession session) {
        //getAttribute得到登录进来人员的地址
        LogisticsStaff logisticsStaff = (LogisticsStaff)session.getAttribute("staff");
        //根据地址queryMapId方法，返回一个集合
        List<LogisticsCar> list = logisticsCarService.queryMapId(logisticsStaff.getMapId());
        return Tools.getLayUiMap(list);
    }

}
